package cn.tqfeiyang.smartadmin.system.mapper;

import cn.tqfeiyang.smartadmin.system.entity.MenuEntity;
import cn.tqfeiyang.smartadmin.system.entity.RoleEntity;
import cn.tqfeiyang.smartadmin.system.entity.RoleMenuEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Set;

/**
 * 角色-菜单映射接口
 *
 * @author tqfeiyang
 * @since 2024/8/20 14:15
 */
@Mapper
public interface RoleMenuMapper extends BaseMapper<RoleMenuEntity> {

    @Select("SELECT menu_id FROM sys_role_menu WHERE role_id = #{roleId}")
    Set<Long> selectMenuIdsByRoleId(Long roleId);

    @Select("SELECT m.* FROM sys_role_menu rm JOIN sys_menu m ON m.menu_id = rm.menu_id WHERE rm.role_id = #{roleId}")
    List<MenuEntity> selectMenuListByRoleId(Long roleId);

    @Insert("<script>"
            + "INSERT INTO sys_role_menu(role_id, menu_id) "
            + "SELECT #{roleId}, menu_id "
            + "FROM sys_menu "
            + "WHERE menu_id IN "
            + "<foreach item='id' index='index' collection='menuIds' open='(' separator=',' close=')'>"
            + "#{id}"
            + "</foreach>"
            + "</script>")
    int insertMenuIdsByRoleId(Long roleId, Set<Long> menuIds);

//    @Delete("<script>"
//            + "DELETE FROM sys_role_menu "
//            + "WHERE role_id = #{roleId} "
//            + "AND menu_id IN "
//            + "<foreach item='id' index='index' collection='menuIds' open='(' separator=',' close=')'>"
//            + "#{id}"
//            + "</foreach>"
//            + "</script>")
//    int deleteMenuIdsByRoleId(Long roleId, Set<Long> menuIds);

    @Delete("DELETE FROM sys_role_menu WHERE role_id = #{roleId}")
    void deleteMenuIdsByRoleId(Long roleId);

    @Select("SELECT role_id FROM sys_role_menu WHERE menu_id = #{menuId}")
    Set<Long> selectRoleIdsByMenuId(Long menuId);

    @Select("SELECT r.* FROM sys_role_menu rm JOIN sys_role r ON r.role_id = rm.role_id WHERE rm.menu_id = #{menuId}")
    List<RoleEntity> selectRoleListByMenuId(Long menuId);

    @Insert("<script>"
            + "INSERT INTO sys_role_menu(menu_id, role_id) "
            + "SELECT #{menuId}, role_id "
            + "FROM sys_role "
            + "WHERE role_id IN "
            + "<foreach item='id' index='index' collection='roleIds' open='(' separator=',' close=')'>"
            + "#{id}"
            + "</foreach>"
            + "</script>")
    int insertRoleIdsByMenuId(Long menuId, Set<Long> roleIds);

//    @Delete("<script>"
//            + "DELETE FROM sys_role_menu "
//            + "WHERE menu_id = #{menuId} "
//            + "AND role_id IN "
//            + "<foreach item='id' index='index' collection='roleIds' open='(' separator=',' close=')'>"
//            + "#{id}"
//            + "</foreach>"
//            + "</script>")
//    int deleteRoleIdsByMenuId(Long menuId, Set<Long> roleIds);

    @Delete("DELETE FROM sys_role_menu WHERE menu_id = #{menuId}")
    void deleteRoleIdsByMenuId(Long menuId);

}
